# coding:utf-8
# Import the common package
import os
import unittest
from appium import webdriver
from time import sleep

# 设置路径信息
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


class LoginAndroidTests(unittest.TestCase):
    def __init__(self):
        pass

    def setUp(self):
        # 初始化测试平台
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '9.0.0'  # Android版本
        # desired_caps['deviceName'] = '127.0.0.1:62001'　　  # 连接到的设备名称，这个是夜神模拟器的名称
        desired_caps['deviceName'] = 'HUAWEI Mate10'  # 设备名称 3HX5T17415004741
        # desired_caps['app'] = 'D:\apk\爱壁纸.apk'　　#app的路径
        # desired_caps['app'] = "D:\wjj\app_package\3.4.1_341283_201905050958-release.apk" #app的路径
        desired_caps['appPackage'] = 'com.sensetime.iva.senseface'  # 包名
        desired_caps['appActivity'] = 'com.sensetime.iva.ui.login.LoginActivity' #APP登陆页的Activity
        return desired_caps

    def tearDown(self):
        self.driver.quit()

    def test(self):
        # 测试登录页
        print("start test...")


        # 判断是否安装APP
        wallpaper = self.driver.is_app_installed("com.sensetime.iva.senseface")
        desired_caps = LoginAndroidTests.setUp()
        if wallpaper:
            self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
            # self.driver.remove_app("com.sensetime.iva.senseface")
            #启动APP
            self.driver.launch_app()

            sleep(8)
            #点击用户名输入框,并输入账号
            self.driver.find_element_by_id('com.sensetime.iva.senseface:id/input_login_username').clear().send_keys("wjj1")
            # driver.find_elements_by_class_name("android.widget.EditText")[1].clear().send_keys("wjj1")

            sleep(4)
            # 点击密码输入框并输入密码
            self.driver.find_element_by_id('com.sensetime.iva.senseface:id/input_login_pwd').clear().send_keys("wjj123456")
            # driver.find_elements_by_class_name("android.widget.EditText")[2].clear().send_keys("wjj123456")
            sleep(5)
            # 点击IP输入框
            self.driver.find_element_by_id("com.sensetime.iva.senseface:id/input_login_ip").click()
            #隐藏键盘
            self.driver.hide_keyboard()
            #在IP输入框中输入ip各字段
            self.driver.find_element_by_id("com.sensetime.iva.senseface: id / ip_1").send_keys("10")
            sleep(5)
            self.driver.find_element_by_id("com.sensetime.iva.senseface: id / ip_2").send_keys("111")
            sleep(5)
            self.driver.find_element_by_id("com.sensetime.iva.senseface: id / ip_3").send_keys("32")
            sleep(5)
            self.driver.find_element_by_id("com.sensetime.iva.senseface: id / ip_4").send_keys("70")
            sleep(5)
            #点击“确定”
            self.driver.find_element_by_id("com.sensetime.iva.senseface:id/tv_sure").click()
            sleep(5)
            # 点击“登录”
            self.driver.find_element_by_id("com.sensetime.iva.senseface:id/btn_login").click()
            sleep(5)
        else:
            self.driver.install_app("D:\wjj\app_package\3.4.1_341283_201905050958-release.apk")
            sleep(30)


if __name__ == '__main__':

    suite = unittest.TestLoader().loadTestsFromTestCase(LoginAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)